package message

import (
	"context"
	"encoding/json"
	"github.com/gogf/gf/v2/util/gconv"
	"home-butler/api/message"
	v1 "home-butler/api/message/v1"
	"home-butler/internal/model"
	"home-butler/pkg/asynqwork"
)

type ControllerV1 struct{}

func NewV1() message.IMessageV1 {
	return &ControllerV1{}
}

func (c ControllerV1) Message(ctx context.Context, req *v1.MessageReq) (res *v1.MessageRes, err error) {
	/*log.Println(req.ClientId)
	log.Println(req.Topic)
	log.Println(req.UserName)
	log.Println(req.Payload)*/
	//_, _, err = service.Product().FindGetMetadata(ctx, 1794219939016478720)
	//result := utility.Parsing(req.Payload)
	//fmt.Println(result["Device"].String())
	data := model.MessageReq{}
	err = gconv.Struct(req, &data)
	if err != nil {
		panic(err)
	}

	//rdb, err := redis.RedisClient()
	//reqBytes, err := json.Marshal(req)
	//s := string(reqBytes)
	//rdb.Publish(ctx, "device_channel", s)

	//if err = service.Message().MessageParsing(ctx, &data); err != nil {
	//	panic(err)
	//}
	dataJson, _ := json.Marshal(data)
	asynqwork.ScheduledMessageUp.Push(ctx, "message_up_channel", dataJson, 10)
	return
}
